﻿SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON

GO


CREATE PROCEDURE [dbo].[bhp_temp_AddBlogStructure]
AS
BEGIN

	DECLARE
		@BlogTabId				INT,
		@BlogTagsTabId		INT,
		@Sort						INT,
		@TabId					INT


	SET @Sort = 0

	EXECUTE [dbo].[n_TableAdd] 'BLOG_POSTS', 900900
	EXECUTE [dbo].[n_TableAdd] 'BLOG_TAGS', 900900
	
	SELECT @BlogTabId = id FROM n_Tree t WHERE t.name = 'BLOG_POSTS'
	SELECT @BlogTagsTabId = id FROM n_Tree t WHERE t.name = 'BLOG_TAGS'
	
	-- BLOG_POSTS.title
	SET @Sort = @Sort + 1
	EXEC [dbo].[n_ColFAdd] @rel = @BlogTabId, @name = N'title', @typ = 2, @siz1 = 1000, @siz2 = 1, @user = 900900
	ALTER TABLE BLOG_POSTS ALTER COLUMN title nvarchar(1000) NOT NULL
	INSERT INTO bht_Columns (ColumnId, DisplayName, [Description], Sort)
	SELECT
		id, N'Tytuł', N'', @Sort
	FROM
		n_Tree t
	WHERE
		parent = @BlogTabId
		AND name = N'title'
	
	-- BLOG_POSTS.url
	SET @Sort = @Sort + 1
	EXEC [dbo].[n_ColFAdd] @rel = @BlogTabId, @name = N'url', @typ = 2, @siz1 = 1000, @siz2 = 1, @user = 900900
	INSERT INTO bht_Columns (ColumnId, DisplayName, [Description], Sort)
	SELECT
		id, N'Adres posta', N'Adres url pod jakim powinien pojawić się post. Jeżeli nie wiesz co wpisać to zostaw - uzupełnimy to automatycznie', @Sort
	FROM
		n_Tree t
	WHERE
		parent = @BlogTabId
		AND name = N'url'
	
	-- BLOG_POSTS.lid
	SET @Sort = @Sort + 1
	EXEC [dbo].[n_ColFAdd] @rel = @BlogTabId, @name = N'lid', @typ = 2, @siz1 = 8000, @siz2 = 1, @user = 900900
	ALTER TABLE BLOG_POSTS ALTER COLUMN lid nvarchar(MAX) NOT NULL
	INSERT INTO bht_Columns (ColumnId, DisplayName, [Description], Sort, CustomEditControl)
	SELECT
		id, N'Wstęp do posta', N'Krótki opis co znajdziemy w pełnej treści posta', @Sort, N'blackhouse.WebNemo.Cms.Nemo.Controls.HtmlEditControl,blackhouse.WebNemo.Cms'
	FROM
		n_Tree t
	WHERE
		parent = @BlogTabId
		AND name = N'lid'

	-- BLOG_POSTS.content
	SET @Sort = @Sort + 1
	EXEC [dbo].[n_ColFAdd] @rel = @BlogTabId, @name = N'content', @typ = 2, @siz1 = 8000, @siz2 = 1, @user = 900900
	ALTER TABLE BLOG_POSTS ALTER COLUMN content nvarchar(MAX) NOT NULL
	INSERT INTO bht_Columns (ColumnId, DisplayName, [Description], Sort, CustomEditControl)
	SELECT
		id, N'Wstęp do posta', N'Krótki opis co znajdziemy w pełnej treści posta', @Sort, N'blackhouse.WebNemo.Cms.Nemo.Controls.HtmlEditControl,blackhouse.WebNemo.Cms'
	FROM
		n_Tree t
	WHERE
		parent = @BlogTabId
		AND name = N'content'

	-- BLOG_POSTS.tags
	SET @Sort = @Sort + 1
	EXEC [dbo].[n_ColFNAdd] @tab = @BlogTabId, @name = N'tags', @rel = @BlogTagsTabId, @user = 900900
	INSERT INTO bht_Columns (ColumnId, DisplayName, [Description], Sort)
	SELECT
		id, N'Tagi', N'Oznacz posta tagami, poprawi to ich indeksację i pomoże "zwiększyć" prawdopodobieństwo zbudowania dobrej chmury tagów twojego bloga', @Sort
	FROM
		n_Tree t
	WHERE
		parent = @BlogTabId
		AND name = N'tags'
		
	-- BLOG_POSTS.moduleId
	SET @Sort = @Sort + 1
	EXEC [dbo].[n_ColFAdd] @rel = @BlogTabId, @name = N'moduleId', @typ = 1, @siz1 = 1000, @siz2 = 1, @user = 900900
	INSERT INTO bht_Columns (ColumnId, DisplayName, HideForEdition)
	SELECT
		id, N'Place ID', 1
	FROM
		n_Tree t
	WHERE
		parent = @BlogTabId
		AND name = N'moduleId'
		
	-- BLOG_POSTS.publishDate
	SET @Sort = @Sort + 1
	EXEC [dbo].[n_ColFAdd] @rel = @BlogTabId, @name = N'publishDate', @typ = 7, @siz1 = 1000, @siz2 = 1, @user = 900900
	INSERT INTO bht_Columns (ColumnId, DisplayName, HideForEdition)
	SELECT
		id, N'Data publikacji', 1
	FROM
		n_Tree t
	WHERE
		parent = @BlogTabId
		AND name = N'publishDate'
		
	-- BLOG_POSTS.published
	SET @Sort = @Sort + 1
	EXEC [dbo].[n_ColFAdd] @rel = @BlogTabId, @name = N'published', @typ = 5, @siz1 = 1000, @siz2 = 1, @user = 900900
	INSERT INTO bht_Columns (ColumnId, DisplayName, HideForEdition)
	SELECT
		id, N'Post opublikowany', 1
	FROM
		n_Tree t
	WHERE
		parent = @BlogTabId
		AND name = N'published'

	SET @Sort = 0
	-- BLOG_TAGS.tagName
	SET @Sort = @Sort + 1
	EXEC [dbo].[n_ColFAdd] @rel = @BlogTagsTabId, @name = N'tagName', @typ = 2, @siz1 = 1000, @siz2 = 1, @user = 900900
	ALTER TABLE BLOG_TAGS ALTER COLUMN tagName nvarchar(50) NOT NULL
	INSERT INTO bht_Columns (ColumnId, DisplayName, [Description], Sort)
	SELECT
		id, N'Nazwa tagu', N'', @Sort
	FROM
		n_Tree t
	WHERE
		parent = @BlogTagsTabId
		AND name = N'tagName'

	-- BLOG_TAGS.url
	SET @Sort = @Sort + 1
	EXEC [dbo].[n_ColFAdd] @rel = @BlogTagsTabId, @name = N'url', @typ = 2, @siz1 = 1000, @siz2 = 1, @user = 900900
	INSERT INTO bht_Columns (ColumnId, DisplayName, HideForEdition)
	SELECT
		id, N'URL', 1
	FROM
		n_Tree t
	WHERE
		parent = @BlogTagsTabId
		AND name = N'url'

	SET @Sort = 0
	-- BLOG_TAGS.title
	SET @Sort = @Sort + 1
	EXEC [dbo].[n_ColFAdd] @rel = @BlogTagsTabId, @name = N'title', @typ = 2, @siz1 = 1000, @siz2 = 1, @user = 900900
	ALTER TABLE BLOG_TAGS ALTER COLUMN title nvarchar(400) NOT NULL
	INSERT INTO bht_Columns (ColumnId, DisplayName, [Description], Sort)
	SELECT
		id, N'Tytuł dla taga', N'Tytuł służy do optymalizacji strony z wynikami wyszukiwania tagu', @Sort
	FROM
		n_Tree t
	WHERE
		parent = @BlogTagsTabId
		AND name = N'title'

	-- USTAWIENIE TABSÓW DLA BLOG_POSTS
	INSERT INTO bht_GroupTabs (TabName, Sort)
		VALUES ('Podstawowe informacje o poście', 100)
		
	SELECT @TabId = SCOPE_IDENTITY()
	
	INSERT INTO bht_GroupColumnsSplit (ColumnId, TabId)
		SELECT id, @TabId FROM n_Tree WHERE name IN ('title', 'tags', 'url') AND parent = @BlogTabId

		INSERT INTO bht_GroupTabs (TabName, Sort)
		VALUES ('Treść', 200)
		
	SELECT @TabId = SCOPE_IDENTITY()
	
	INSERT INTO bht_GroupColumnsSplit (ColumnId, TabId)
		SELECT id, @TabId FROM n_Tree WHERE name IN ('lid', 'content') AND parent = @BlogTabId

END
GO

EXEC [bhp_temp_AddBlogStructure]
GO

DROP PROCEDURE [bhp_temp_AddBlogStructure]